package com.jiejie.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTCreationException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.jiejie.entity.LoginCustomer;

import java.util.HashMap;
import java.util.Map;


public class TokenUtil {

    //token到期时间60s
    private static final long EXPIRE_TIME= 600*1000;
    //密钥盐
    private static final String TOKEN_SECRET="123456qwertyuiop789";

    /**
     * 创建一个token
     * @param LoginCustomer
     * @return 生成的token中不带有过期时间，token的过期时间由redis进行管理
     */
    public static String sign(LoginCustomer LoginCustomer){
        System.out.println("sign customer:" + LoginCustomer);
        String token=null;
        try {
            Map<String, Object> header = new HashMap<>(2);
            header.put("Type", "Jwt");
            header.put("alg", "HS256");
            token = JWT.create()
                    .withHeader(header)
                    .withClaim("token", JSONObject.toJSONString(LoginCustomer))//存放数据
                    .sign(Algorithm.HMAC256(TOKEN_SECRET));
        } catch (IllegalArgumentException | JWTCreationException je) {
            je.printStackTrace();

        }
        return token;
    }
    /**
     * 对token进行验证
     * @param token
     * @return
     */
    public static LoginCustomer parseToken(String token){
        Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
        JWTVerifier verifier = JWT.require(algorithm).build();
        DecodedJWT decodedJWT = verifier.verify(token);

        String tokenInfo = decodedJWT.getClaim("token").asString();
        LoginCustomer LoginCustomer = JSON.parseObject(tokenInfo, LoginCustomer.class);
        System.out.println("获得的token中的信息是：" + LoginCustomer);
        return LoginCustomer;
    }




















    /*//token到期时间60s
    private static final long EXPIRE_TIME=600*1000;
    //密钥盐
    private static final String TOKEN_SECRET="123456qwertyuiop789";

    *//**
     * 创建一个token
     *//*
    public static String sign(Customer user){
        System.out.println("sign customer:" + user);
        String token=null;
        Date expireAt=new Date(System.currentTimeMillis()+EXPIRE_TIME);
        token= JWT.create()
                .withIssuer("auth0")
                .withClaim("customerId",user.getCustomerId())
                .withClaim("customerName",user.getCustomerName())
                .withClaim("customerPassword",user.getCustomerPassword())
                .withExpiresAt(expireAt)
                .sign(Algorithm.HMAC256(TOKEN_SECRET));
        return token;
    }

    *//**
     * 对token进行验证
     *//*
    public static Boolean verify(String token){
        //创建token验证器
        JWTVerifier jwtVerifier=JWT.require(Algorithm.HMAC256(TOKEN_SECRET)).withIssuer("auth0").build();
        DecodedJWT decodedJWT=jwtVerifier.verify(token);

        System.out.println("认证通过：");
        System.out.println("customerId: " + TokenUtil.getUserId(token));
        System.out.println("customerName: " + TokenUtil.getUserName(token));
        System.out.println("过期时间： " + decodedJWT.getExpiresAt());

        return true;
    }

    *//**
     * 获取用户名
     *//*
    public static String getUserName(String token){
        DecodedJWT jwt=JWT.decode(token);
        String username=jwt.getClaim("customerName").asString();
        System.out.println("用户名:" + username);
        return username;
    }

    public static Integer getUserId(String token){
        DecodedJWT jwt=JWT.decode(token);
        Integer userId=jwt.getClaim("customerId").asInt();
        System.out.println("用户id:" + userId);
        return  userId ;
    }*/

}
